//
// Created by pengshuai on 2021/6/22.
//

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "tobii_research.h"
#include "tobii_research_eyetracker.h"
#include "tobii_research_streams.h"
#if _WIN32 || _WIN64
#include <windows.h>
static void sleep_ms(int time) {
    Sleep(time);
}
#else
#include <unistd.h>
static void sleep_ms(int time) {
    usleep(time * 1000);
}
#endif

TobiiResearchEyeTracker *eyeTracker;


TobiiResearchStatus find_all_eyetrackers_example() {
    TobiiResearchEyeTrackers* eyetrackers = NULL;
    TobiiResearchStatus result;
    size_t i;
    result = tobii_research_find_all_eyetrackers(&eyetrackers);
    if (result != TOBII_RESEARCH_STATUS_OK) {
        printf("Finding trackers failed. Error: %d\n", result);
        return result;
    }
    for (i = 0; i < eyetrackers->count; i++) {
        TobiiResearchEyeTracker* eyetracker = eyetrackers->eyetrackers[i];
        eyeTracker = eyetracker;
        char* address;
        char* serial_number;
        char* device_name;
        tobii_research_get_address(eyetracker, &address);
        tobii_research_get_serial_number(eyetracker, &serial_number);
        tobii_research_get_device_name(eyetracker, &device_name);
        printf("%s\t%s\t%s\n", address, serial_number, device_name);
        tobii_research_free_string(address);
        tobii_research_free_string(serial_number);
        tobii_research_free_string(device_name);
    }
    printf("Found %d Eye Trackers \n\n", (int)eyetrackers->count);
    tobii_research_free_eyetrackers(eyetrackers);
    return result;
}


void gaze_data_callback(TobiiResearchGazeData* gaze_data, void* user_data) {
    memcpy(user_data, gaze_data, sizeof(*gaze_data));

}

void gaze_data_example(TobiiResearchEyeTracker* eyetracker) {
    TobiiResearchGazeData gaze_data;
    char* serial_number;
    tobii_research_get_serial_number(eyetracker, &serial_number);
    printf("Subscribing to gaze data for eye tracker with serial number %s.\n", serial_number);
    tobii_research_free_string(serial_number);
    TobiiResearchStatus status = tobii_research_subscribe_to_gaze_data(eyetracker, gaze_data_callback, &gaze_data);
    if (status != TOBII_RESEARCH_STATUS_OK)
        return;
    /* Wait while some gaze data is collected. */
    sleep_ms(2000);
    status = tobii_research_unsubscribe_from_gaze_data(eyetracker, gaze_data_callback);
    printf("Unsubscribed from gaze data with status %i.\n", status);
    printf("Last received gaze package:\n");
    printf("System time stamp: %"  PRId64 "\n", gaze_data.system_time_stamp);
    printf("Device time stamp: %"  PRId64 "\n", gaze_data.device_time_stamp);
    printf("Left eye 2D gaze point on display area: (%f, %f)\n",
           gaze_data.left_eye.gaze_point.position_on_display_area.x,
           gaze_data.left_eye.gaze_point.position_on_display_area.y);
    printf("Right eye 3d gaze origin in user coordinates (%f, %f, %f)\n",
           gaze_data.right_eye.gaze_origin.position_in_user_coordinates.x,
           gaze_data.right_eye.gaze_origin.position_in_user_coordinates.y,
           gaze_data.right_eye.gaze_origin.position_in_user_coordinates.z);
    /* Wait while some gaze data is collected. */
    sleep_ms(2000);
}
/*
void gaze_output_frequencies_example(TobiiResearchEyeTracker* eyetracker) {
    float initial_gaze_output_frequency;
    TobiiResearchStatus status = tobii_research_get_gaze_output_frequency(eyetracker, &initial_gaze_output_frequency);
    printf("The eye tracker's initial gaze output frequency is %f Hz with status %i.\n",
           initial_gaze_output_frequency, status);
    {
        TobiiResearchGazeOutputFrequencies* frequencies = NULL;
        size_t i = 0;
        status = tobii_research_get_all_gaze_output_frequencies(eyetracker, &frequencies);
        for(; i < frequencies->frequency_count; i++) {
            status = tobii_research_set_gaze_output_frequency(eyetracker, frequencies->frequencies[i]);
            printf("Gaze output frequency set to %f Hz with status %i.\n", frequencies->frequencies[i], status);
        }
        tobii_research_set_gaze_output_frequency(eyetracker, initial_gaze_output_frequency);
        printf("Gaze output frequency reset to %f Hz.", initial_gaze_output_frequency);
        tobii_research_free_gaze_output_frequencies(frequencies);
    }
}
*/

int main()
{
    printf("8888");
    TobiiResearchStatus result = find_all_eyetrackers_example();
    printf("%i", result);
    gaze_data_example(eyeTracker);

    return 0;
}

